package cn.iocoder.yudao.module.course.controller.admin.lessoncourse;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.course.controller.admin.lessoncourse.vo.*;
import cn.iocoder.yudao.module.course.dal.dataobject.lessoncourse.LessonCourseDO;
import cn.iocoder.yudao.module.course.convert.lessoncourse.LessonCourseConvert;
import cn.iocoder.yudao.module.course.service.lessoncourse.LessonCourseService;

@Tag(name = "管理后台 - 课时管理， 每个课程里面多少个课时")
@RestController
@RequestMapping("/qhsx/lesson-course")
@Validated
public class LessonCourseController {

    @Resource
    private LessonCourseService lessonCourseService;

    @PostMapping("/create")
    @Operation(summary = "创建课时管理， 每个课程里面多少个课时")
    @PreAuthorize("@ss.hasPermission('qhsx:lesson-course:create')")
    public CommonResult<Long> createLessonCourse(@Valid @RequestBody LessonCourseCreateReqVO createReqVO) {
        return success(lessonCourseService.createLessonCourse(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新课时管理， 每个课程里面多少个课时")
    @PreAuthorize("@ss.hasPermission('qhsx:lesson-course:update')")
    public CommonResult<Boolean> updateLessonCourse(@Valid @RequestBody LessonCourseUpdateReqVO updateReqVO) {
        lessonCourseService.updateLessonCourse(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除课时管理， 每个课程里面多少个课时")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('qhsx:lesson-course:delete')")
    public CommonResult<Boolean> deleteLessonCourse(@RequestParam("id") Long id) {
        lessonCourseService.deleteLessonCourse(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得课时管理， 每个课程里面多少个课时")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('qhsx:lesson-course:query')")
    public CommonResult<LessonCourseRespVO> getLessonCourse(@RequestParam("id") Long id) {
        LessonCourseDO lessonCourse = lessonCourseService.getLessonCourse(id);
        return success(LessonCourseConvert.INSTANCE.convert(lessonCourse));
    }

    @GetMapping("/list")
    @Operation(summary = "获得课时管理， 每个课程里面多少个课时列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('qhsx:lesson-course:query')")
    public CommonResult<List<LessonCourseRespVO>> getLessonCourseList(@RequestParam("ids") Collection<Long> ids) {
        List<LessonCourseDO> list = lessonCourseService.getLessonCourseList(ids);
        return success(LessonCourseConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得课时管理， 每个课程里面多少个课时分页")
    @PreAuthorize("@ss.hasPermission('qhsx:lesson-course:query')")
    public CommonResult<PageResult<LessonCourseRespVO>> getLessonCoursePage(@Valid LessonCoursePageReqVO pageVO) {
        PageResult<LessonCourseDO> pageResult = lessonCourseService.getLessonCoursePage(pageVO);
        return success(LessonCourseConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出课时管理， 每个课程里面多少个课时 Excel")
    @PreAuthorize("@ss.hasPermission('qhsx:lesson-course:export')")
    @OperateLog(type = EXPORT)
    public void exportLessonCourseExcel(@Valid LessonCourseExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<LessonCourseDO> list = lessonCourseService.getLessonCourseList(exportReqVO);
        // 导出 Excel
        List<LessonCourseExcelVO> datas = LessonCourseConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "课时管理， 每个课程里面多少个课时.xls", "数据", LessonCourseExcelVO.class, datas);
    }

}
